package org.example.controller;

import org.apache.dubbo.config.annotation.Reference;
import org.example.model.Token;
import org.example.service.CodeService2;
import org.example.service.UserService;
import org.example.service.UserService2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Optional;
import java.util.UUID;

@RestController
@RequestMapping("/user")
public class UserController {

    @Reference
    CodeService2 codeService2;

    @Autowired
    private UserService2 userService2;

    @Autowired
    UserService userService;

    @GetMapping("/register/{email}/{password}/{code}")
    public int register(@PathVariable("email") String email,
                            @PathVariable("password")String password,
                            @PathVariable("code")String code){
        System.out.println("user register: email="+email+",password="+password+",code="+code);
        if(!isRegistered(email)){
            int res = codeService2.validateEmail(email, code);
            System.out.println(res);
            if(res==0){
                Token t = new Token();
                t.setEmail(email);
                userService.addToken(t);
            }
            return res;
        }
        return 0;
    }

    @GetMapping("/isRegistered/{email}")
    public boolean isRegistered(@PathVariable("email") String email){
        System.out.println("email="+email);
        Token t = new Token();
        t.setEmail(email);
        Optional<Token> optional = userService.findByEmail(t);
        return optional.isPresent();
    }

    @GetMapping("/login/{email}/{password}")
    public String login(HttpServletRequest request,
                        @PathVariable("email") String email,
                        @PathVariable("password") String password,
                        HttpServletResponse response){
        System.out.println("login email="+email+" password="+password);
        Token t = new Token();
        t.setEmail(email);
        Optional<Token> optional = userService.findByEmail(t);
        if(optional.isPresent() && password.equals("password")){
            // password should always be 'password'
            Token res = optional.get();
            // generate a UUID to mock a token
            String token = UUID.randomUUID().toString();
            res.setToken(token);
            userService.addToken(res);
            // add token in cookie
            Cookie cookie = new Cookie("token", token);
            cookie.setPath("/");
            response.addCookie(cookie);
            return res.getEmail();
        }
        response.setStatus(401);
        return "user not found";
    }

    @GetMapping("/info/{token}")
    public String info(@PathVariable("token") String token, HttpServletResponse res){
        String msg = userService2.info(token);
        if(msg.equals("token not valid")){
            res.setStatus(401);
        }
        return msg;
    }
}
